import 'package:app/home_module/all_grid_model.dart';
import 'package:bot_toast/bot_toast.dart';
import 'package:flutter/cupertino.dart';
import 'package:flutter/material.dart';
import 'package:flutter_easyrefresh/easy_refresh.dart';

import '../common_widget/colors.dart';
import '../net/address.dart';
import '../net/dio_manager.dart';
import 'app_grid_model.dart';
import 'package:get/get.dart';
class AllModulePage extends StatefulWidget {
  const AllModulePage({Key? key}) : super(key: key);

  @override
  State<AllModulePage> createState() => _AllModulePageState();
}

class _AllModulePageState extends State<AllModulePage> {




  AppGridModel? _model;

  bool isEdit = false;

  /// 获取我的全部
  requestData()async{

    var params  = {
      'page':'1',
      'limit':'100',
    };

    var json = await DioManager().kkRequest(Address.appGrid,params: params,method: 'get');

    AppGridModel model = AppGridModel.fromJson(json);

    _model = model;

    setState(() {

    });
  }
  /// 添加关注
  requestDataWithFocus(var grid_id)async{
    var params = {
      'grid_id':grid_id,
    };
    var json = await DioManager().kkRequest(Address.appGridFocus,bodyParams:params);

    BotToast.showText(text: '${json['message']}');

  }
  @override
  void initState() {
    // TODO: implement initState
    super.initState();
    requestData();

  }

  @override
  Widget build(BuildContext context) {
    return Scaffold(
      appBar: AppBar(
        title: Text('全部',style: TextStyle(color: Colors.black,fontSize: 16),),
        actions: [
          TextButton(onPressed: (){
            isEdit =! isEdit;
            setState(() {

            });
          }, child: isEdit==false?Text('编辑'):Text('完成'))
        ],
      ),
      body: EasyRefresh.custom(
        slivers: [

          SliverList(delegate: _mySliverChildBuilderDelegate(),),


        ],
      ),
    );
  }


  _mySliverChildBuilderDelegate() {
    return SliverChildBuilderDelegate(
          (BuildContext context, int index) {
        return Container(
          decoration: BoxDecoration(
            color: Colors.white,
            borderRadius: BorderRadius.all(Radius.circular(8)),
          ),
          margin: EdgeInsets.only(top: 10,left: 15,right: 15),
          child: Column(
            crossAxisAlignment: CrossAxisAlignment.start,
            children: [
              Container(
                // color: Colors.red,
                  width: Get.width-15,
                  padding: EdgeInsets.only(top: 10,left: 10,right: 10),
                  child:Row(
                    mainAxisAlignment: MainAxisAlignment.spaceBetween,
                    children: [
                      Text('${_model?.data?[index].categoryName}',style: TextStyle(fontWeight: FontWeight.w500,fontSize: 16),),
                    ],
                  )
              ),
              SizedBox(height: 15,),
              GridView.builder(
                physics: const NeverScrollableScrollPhysics(),
                shrinkWrap: true,
                gridDelegate: _mySliverGridDelegateWithFixedCrossAxisCount(),
                itemBuilder: (BuildContext context, int a) {
                  return Container(
                    height: 50,
                    margin: EdgeInsets.only(top: 5,left: 10),
                    // color: Colors.orange,
                    // color: Colors.primaries[index % 8],
                    child: Column(
                      children: [
                        Stack(
                          alignment: Alignment.topRight,
                          children: [
                            Image.asset('images/demo_icon.png'),
                         isEdit==false?Container():GestureDetector(
                              child: Image.asset('images/add_icon.png',
                                width: 15,height: 15,color: AppColor.themeColor,),
                              onTap: (){
                                requestDataWithFocus(_model?.data?[index].apps?[a].id);
                              },
                            )
                          ],
                        ),
                        Text('${_model?.data?[index].apps?[a].name}',style: TextStyle(fontSize: 12),)
                      ],
                    ),
                  );
                },itemCount: _model?.data?[index].apps?.length??0,),
            ],
          ),
        );
      },
      childCount: _model?.data?.length??0,
    );
  }

  _mySliverGridDelegateWithFixedCrossAxisCount() {
    return SliverGridDelegateWithFixedCrossAxisCount(
      crossAxisCount: 5,
      mainAxisSpacing: 10,
      crossAxisSpacing: 5,
      childAspectRatio: 1/1.2,
    );
  }
}
